import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
export const useDictsStore = defineStore('dicts', () => {
	let dictList: any = {};

	function setDict(name: string) {
		return new Promise((resolve, reject) => {
			if (dictList[name] === void 0) {
				dictList[name] = ref([]);
				setTimeout(() => {
					dictList[name].value = [
						{
							label: Math.random() + 1,
							value: Math.random() + 6,
						},
					];
					resolve(dictList[name].value);
				}, 3000);
			} else {
				resolve(dictList[name].value);
			}
		});
	}

	const getDict = computed(() => (type: string, isPromise = false) => {
		console.log(type);
		if (isPromise) {
			return setDict(type);
		}
		if (dictList[type] === void 0) {
			dictList[type] = ref([]);
			setTimeout(() => {
				dictList[type].value = [
					{
						label: Math.random() + 1,
						value: Math.random() + 6,
					},
				];
			}, 3000);
		}
		return dictList[type];
	});

	return {
		dictList,
		getDict,
	};
});
